<template>
  <div style="width: 100%; height: 100vh; background-image: url('/static/background/weather.png'); overflow: hidden">
    <div style="width: 400px;margin: 140px auto;">
      <div class="formStyle">
        <div style="color: #409EFF; font-size: 25px;font-weight: bold; text-align: center;padding: 30px 0">欢迎注册</div>
        <el-form ref="form" :rules="rules" :model="form" size="normal" >
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user-solid" v-model="form.username" clearable placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" v-model="form.password" clearable placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-form-item prop="confirm">
            <el-input prefix-icon="el-icon-lock" v-model="form.confirm" clearable placeholder="请确认密码" show-password></el-input>
          </el-form-item>
        </el-form>
        <div style="display: flex;justify-content: space-around;margin-bottom: 15px">
          <el-button style="width: 48%" type="primary" round plain @click="register">注册</el-button>
          <el-button style="width: 48%" type="danger" round plain @click="goToBack">返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data(){
    return{
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        confirm: [
          { required: true, message: '请确认密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    register(){
      if (this.form.password !== this.form.confirm){
        this.$message({
          type: "error",
          message: "两次输入密码不一致"
        })
        return
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$axios({
            url:'/user/register',
            method:'post',
            data:this.form
          }).then(({data})=>{
            if (data.code === 0){
              this.$message({
                type: "success",
                message: "注册成功"
              })
              this.$router.push("/")
            }else {
              this.$message({
                type: "error",
                message: data.message
              })
            }
          })
        }
      })
    },
    goToBack(){
      this.$router.back()
    }
  }
}
</script>

<style scoped>
.formStyle {
  height: 50%;
  padding: 10px 20px;
  background-color: white;
  border-radius: 20px;
}
</style>
